package com.pet.exchange;

import com.pet.constant.PetHealthConstant;
import com.pet.context.BaseContext;
import com.pet.entity.*;
import com.pet.mapper.UserInformationMapper;
import com.pet.vo.web.PetHealthInformationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Slf4j
@Component
public class PetHealthUtil {

    @Autowired
    private UserInformationMapper userInformationMapper;
    private Integer getPetAge(Integer petAge){
        //分别判断数字1，2，3，4，5，6，7，8
        return switch (petAge) {
            case 1 ->
                //二月
                    2;
            case 2 ->
                //三月
                    3;
            case 3 ->
                //八月
                    8;
            case 4 ->
                //十二月
                    12;
            case 5 ->
                //一岁到7岁
                    84;
            case 6 ->
                //七岁以上
                    85;
            default -> 0;
        };
    }

    /**
     * 宠物健康信息
     * @param entity
     * @param vo
     * @return
     */
    public PetHealthInformationVO PetHealth(PetHealthInformation entity, PetHealthInformationVO vo){
        //判断宠物类型
        if(entity.getBreed() == 1){//宠物猫
            return null;
        }
        else if (entity.getBreed() == 2){//宠物狗
        entity.setMonth(getPetAge(entity.getMonth()));
            /**
             * 饮食计划
             */
            //创建对应实体
            PetHealthDiet diet = new PetHealthDiet();

            if(entity.getMonth() <= 2){
                diet.setPetCondition(PetHealthConstant.dog.diet.diet_01.petCondition);//宠物年龄阶段
                diet.setStapleFood(PetHealthConstant.dog.diet.diet_01.stapleFood);//主食
                diet.setAssistFood(PetHealthConstant.dog.diet.diet_01.assistFood);//辅食
                diet.setFrequencyFood(PetHealthConstant.dog.diet.diet_01.frequencyFood);//频率
                diet.setNotice(PetHealthConstant.dog.diet.diet_01.notice);//注意事项
            } else if ( entity.getMonth() <= 3) {
                diet.setPetCondition(PetHealthConstant.dog.diet.diet_02.petCondition);
                diet.setStapleFood(PetHealthConstant.dog.diet.diet_02.stapleFood);
                diet.setAssistFood(PetHealthConstant.dog.diet.diet_02.assistFood);
                diet.setFrequencyFood(PetHealthConstant.dog.diet.diet_02.frequencyFood);
                diet.setNotice(PetHealthConstant.dog.diet.diet_02.notice);
            } else if (entity.getMonth() <= 8) {
                diet.setPetCondition(PetHealthConstant.dog.diet.diet_03.petCondition);
                diet.setStapleFood(PetHealthConstant.dog.diet.diet_03.stapleFood);
                diet.setAssistFood(PetHealthConstant.dog.diet.diet_03.assistFood);
                diet.setFrequencyFood(PetHealthConstant.dog.diet.diet_03.frequencyFood);
                diet.setNotice(PetHealthConstant.dog.diet.diet_03.notice);
            } else if (entity.getMonth() <= 12) {
                diet.setPetCondition(PetHealthConstant.dog.diet.diet_04.petCondition);
                diet.setStapleFood(PetHealthConstant.dog.diet.diet_04.stapleFood);
                diet.setAssistFood(PetHealthConstant.dog.diet.diet_04.assistFood);
                diet.setFrequencyFood(PetHealthConstant.dog.diet.diet_04.frequencyFood);
                diet.setNotice(PetHealthConstant.dog.diet.diet_04.notice);
            } else if (entity.getMonth() <= 84) {
                diet.setPetCondition(PetHealthConstant.dog.diet.diet_05.petCondition);
                diet.setStapleFood(PetHealthConstant.dog.diet.diet_05.stapleFood);
                diet.setAssistFood(PetHealthConstant.dog.diet.diet_05.assistFood);
                diet.setFrequencyFood(PetHealthConstant.dog.diet.diet_05.frequencyFood);
                diet.setNotice(PetHealthConstant.dog.diet.diet_05.notice);
            } else {
                diet.setPetCondition(PetHealthConstant.dog.diet.diet_06.petCondition);
                diet.setStapleFood(PetHealthConstant.dog.diet.diet_06.stapleFood);
                diet.setAssistFood(PetHealthConstant.dog.diet.diet_06.assistFood);
                diet.setFrequencyFood(PetHealthConstant.dog.diet.diet_06.frequencyFood);
                diet.setNotice(PetHealthConstant.dog.diet.diet_06.notice);
            }
            vo.setPetHealthDiet(diet);

            /**
             * 运动计划
             */
            //创建对应实体
            PetHealthSport sport = new PetHealthSport();
            if(entity.getKg() <= 5){
                sport.setAmountSport(PetHealthConstant.dog.sport.Sport_1);
            } else if (entity.getKg() <= 10) {
                sport.setAmountSport(PetHealthConstant.dog.sport.Sport_2);
            } else if (entity.getKg() <= 15) {
                sport.setAmountSport(PetHealthConstant.dog.sport.Sport_3);
            } else if (entity.getKg() <= 25) {
                sport.setAmountSport(PetHealthConstant.dog.sport.Sport_4);
            }else{
                sport.setAmountSport(PetHealthConstant.dog.sport.Sport_5);
            }
            sport.setPetSportNotice(PetHealthConstant.dog.sport.notice);
            sport.setSuggestSport(PetHealthConstant.dog.sport.suggestSport);

            vo.setPetHealthSport(sport);


            /**
             * 生活计划
             */
            //提示字段设置
            PetHealthLife life = new PetHealthLife();
            life.setWashTimeReason(PetHealthConstant.dog.life.washTime);
            if (entity.getMonth() <= 12){
                life.setInternalDewormingReason(PetHealthConstant.dog.life.internalDeworming_3_12);
            }else {
                life.setInternalDewormingReason(PetHealthConstant.dog.life.internalDeworming_12);
            }
            life.setExtracorporealDewormingReason(PetHealthConstant.dog.life.extracorporealDeworming);
            life.setToothCleanReason(PetHealthConstant.dog.life.nextToothClean);
            life.setTearStainCleanReason(PetHealthConstant.dog.life.nextTearStainClean);

            //设置计划时间
            //下次洗澡时间
            life.setWashTime(entity.getWashTime().plusDays(7));
            //下次体内驱虫时间
            if (entity.getMonth() <= 12){
                life.setNextInternalDeworming(entity.getInternalDeworming().plusMonths(1));
            }else {
                life.setNextInternalDeworming(entity.getInternalDeworming().plusMonths(3));
            }

            //下次体外驱虫时间
            life.setNextExtracorporealDeworming(entity.getExtracorporealDeworming().plusMonths(3));
            //下次牙齿清洁时间
            life.setNextToothClean(LocalDate.now().plusDays(3));
            //下次泪痕清洁时间
            life.setNextTearStainClean(LocalDate.now().plusDays(1));


            vo.setPetHealthLife(life);

            /**
             *健康建议
             */
            //创建相关实体
            PetHealthAdvice advice = new PetHealthAdvice();
            //判断
            if (entity.getBcs() <=2){
                advice.setKgAdvice(PetHealthConstant.dog.health.bcs.bcs_1);
            } else if (entity.getBcs() <=3) {
                advice.setKgAdvice(PetHealthConstant.dog.health.bcs.bcs_2);
            } else if (entity.getBcs() <=5) {
                advice.setKgAdvice(PetHealthConstant.dog.health.bcs.bcs_3);
            } else if (entity.getBcs() <=7) {
                advice.setKgAdvice(PetHealthConstant.dog.health.bcs.bcs_4);
            } else if (entity.getBcs() <=9) {
                advice.setKgAdvice(PetHealthConstant.dog.health.bcs.bcs_5);
            }
            vo.setPetHealthAdvice(advice);

        }

        return vo;
    }

    /**
     * 宠物健康信息
     * 数值->实体
     */
    public PetHealthInformationVO petHealthInfoExchange(PetHealthInformation e){
        //创建vo
        PetHealthInformationVO vo = new PetHealthInformationVO();
        //赋值
        BeanUtils.copyProperties(e, vo);
        //设置用户名
        vo.setCreateUser(userInformationMapper.selectName(e.getCreateUser()));
        //设置品种（猫、狗）
        if(e.getBreed() == 1){
            vo.setBreed("猫猫");
        }else {
            vo.setBreed("狗狗");
        }
        //设置类型（小，中，大）
        if(e.getType() == 1){
            vo.setType("大型");
        } else if (e.getType() == 2) {
            vo.setType("中型");
        }else {
            vo.setType("小型");
        }

        return vo;
    }
}
